﻿Public Class TEMPLATEBILLING
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub TEMPLATEBILLING_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridTempbilling()

        DesignForm(Me)
        DesignGrid(TEMPBILLINGGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridTempbilling()
        Dim objSelectDataTEMPBILLING As New TBILLING
        Dim TEMPBILLING As DataTable
        TEMPBILLING = connect.GetTable(objSelectDataTEMPBILLING.SelectTempbilling_)
        TEMPBILLINGGrid.PrimaryGrid.DataSource = TEMPBILLING
    End Sub

    Private Sub cleardata()
        TEMPNAMETxt.Text = ""
        Tempbillingstatuscheck.Checked = False
    End Sub

    Private Sub TEMPBILLINGGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles TEMPBILLINGGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            TMPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            TMPIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบหมวดลำดับใบเสร็จ").Value() IsNot DBNull.Value Then
            TEMPNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบหมวดลำดับใบเสร็จ").Value()
        Else
            TEMPNAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            Tempbillingstatuscheck.Checked = True
        Else
            Tempbillingstatuscheck.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE TEMPBILLING SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `TMPID` = '" & TMPIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        addTempbilling.Enabled = False
    End Sub

    Private Sub addTempbilling_Click(sender As Object, e As EventArgs) Handles addTempbilling.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If TEMPNAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New TBILLING
                    objInsertData.TEMPNAME_ = TEMPNAMETxt.Text
                    objInsertData.STATUS_ = Tempbillingstatuscheck.Checked
                    objInsertData.insertTempbillingData()

                    SelectDatagridTempbilling()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editTempbilling_Click(sender As Object, e As EventArgs) Handles editTempbilling.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If TEMPNAMETxt.Text.ToString.Length > 0 And TMPIDTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New TBILLING
                        objeditData.TMPID_ = TMPIDTxt.Text
                        objeditData.TEMPNAME_ = TEMPNAMETxt.Text
                        objeditData.STATUS_ = Tempbillingstatuscheck.Checked
                        objeditData.editTempbillingData()

                        SelectDatagridTempbilling()

                        cleardata()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addTempbilling.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class